package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Transactional
    @Override
    public void save(SetmealDto setmealDto) {
        //1.添加套餐表
        super.save(setmealDto);
        //2.添加中间表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
    /*
        分页查询套餐
     */
    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {
        //套餐的分页条件
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //准备dto的page，用于返回
        Page<SetmealDto> pageInfoDto = new Page<>(page, pageSize);
        //模糊匹配条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //1.查询套餐分页
        super.page(pageInfo,setmealLambdaQueryWrapper);
        //属性拷贝
        BeanUtils.copyProperties(pageInfo,pageInfoDto,"records");
        //2.自己完成records集合的数据改造
        List<SetmealDto> setmealDtoList = pageInfo.getRecords().stream().map(setmeal -> {
            //把每个Setmeal变成 SetmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //还缺少一个CategoryName
            //select * from category where id = #{setmeal.categoryId}
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        //手动设置setmealDto到pageInfoDto中的records属性中
        pageInfoDto.setRecords(setmealDtoList);
        return pageInfoDto;
    }
    /*
        批量删除
        注意事项：我勾了3个套餐，进行批量删除。有1个套餐正在售卖中，2个套餐是禁售状态
        此时3个套餐都不能被删除。
     */
    @Transactional
    @Override
    public void removeByIds(List<Long> ids) {
        //1.判断套餐中是否包含 启用状态的，包含则本次操作失败
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // where status = 1 and id in (ids);
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = super.count(setmealLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //2.删除套餐
        super.removeByIds(ids);
        //3.删除套餐菜品中间表
        LambdaUpdateWrapper<SetmealDish> setmealDishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //delete from setmealDish where setmeal_id in (#{ids})
        setmealDishLambdaUpdateWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaUpdateWrapper);
    }

    @Override
    public List<DishDto> dishs(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishLambdaQueryWrapper.orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        //把setmealdish变成dishdto
        List<DishDto> dishDtoList = list.stream().map(setmealDish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(setmealDish, dishDto);//缺少图片
            Dish dish = dishService.getById(setmealDish.getDishId());
            dishDto.setImage(dish.getImage());
            return dishDto;
        }).collect(Collectors.toList());
        return dishDtoList;
    }
}
